<template>
    <BasicModal
      v-bind="$attrs"
      @register="registerModal"
      show-footer
      width="50%"
      @ok="handleSubmit"
    >
      <template #title>
        {{ isUpdate ? '编辑' : '新增' }}
      </template>
      <template #footer>
        <a-button @click="closeModal" :loading="btnLoading">取消</a-button>
        <a-button
          type="primary"
          @click="handleSubmit(false)"
          :loading="btnLoading"
          >保存</a-button
        >
      </template>
      <BasicForm @register="registerForm" />
    </BasicModal>
  </template>
  <script lang="ts" setup>
  import { ref } from 'vue';
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { BasicForm, useForm } from '@/components/Form/index';
  import { formSchema } from './data';
  import { useMessage } from '@/hooks/web/useMessage';
  import { useThrottleFn } from '@vueuse/core';
  import { defHttp } from '@/utils/http/axios';

  const emit = defineEmits(['success', 'register']);
  let isUpdate = ref(true);
  const btnLoading = ref(true);
  const { createMessage } = useMessage();
  const [
    registerForm,
    { resetFields, setFieldsValue, validate },
  ] = useForm({
    labelWidth: 100,
    schemas: [
      {
        field: 'id',
        component: 'Input',
        label: 'id',
        show: false,
      },
      {
        field: 'name',
        component: 'Input',
        label: '标题',
        componentProps: {
          placeholder: '请输入标题',
        },
        required: true,
      },
      {
        field: 'imageUrl',
        component: 'AvUpload',
        label: '展示图',
        required: true,
      },
      {
        field: 'adLocationId',
        component: 'ApiSelect',
        label: '位置',
        componentProps: {
          api: '/admin/ad/adLocationList',
        },
        required: true,
      },
      {
        field: 'link',
        component: 'Input',
        label: '链接地址',
      },
      {
        field: 'content',
        component: 'RichText',
        label: '内容',
      },{
        field: 'orders',
        component: 'InputNumber',
        label: '排序',
        componentProps: {
          min: 0,
          max: 1000000,
          step: 1,
        },
        required: true,
      }, {
        field: 'isOpen',
        component: 'RadioGroup',
        label: '是否显示',
        componentProps: {
          options: [
            { label: '显示', value: true },
            { label: '隐藏', value: false },
          ],
        },
        required: true,
      }
    ],
    showActionButtonGroup: false,
    baseColProps: { span: 24 },
    mode: 'edit',
  });
  // 文档名称
  const adName = ref();
  const isEdit = ref(false);
  const [registerModal, { closeModal, changeLoading }] =
  useModalInner(async (data) => {
      resetFields();
      debugger
      changeLoading(true);
      btnLoading.value = true;
      isUpdate.value =!!data?.isUpdate;
      if (isUpdate.value) {
        setFieldsValue({ ...data.record });
      }
      changeLoading(false);
      btnLoading.value = false;
    });
  const handleSubmit = useThrottleFn(doSubmit, 1000);
  async function doSubmit(hasNext: boolean) {
    try {
      changeLoading(true);
      btnLoading.value = true;
      const values = await validate();
      values.pkRole = values.pkRole?.join(',');
      if (isUpdate.value) {
        const res = await defHttp.post({ url: '/admin/ad/edit', data: values });
        createMessage.success(res);
      } else {
        const res = await defHttp.post({ url: '/admin/ad/add', data: values });
        createMessage.success(res);
      }
      emit('success');
      closeModal();
    } finally {
      btnLoading.value = false;
      changeLoading(false);
    }
  }
  </script>
  